package com.solr;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import java.util.List;
import java.util.Map;

/**
 * @program: Solr
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-03-19
 **/
public class Solrfuxi3 {
    public static final String BASE_URL="http://192.168.117.131:8080/solr/collection1";

    public static HttpSolrClient getSolrClient(){
        HttpSolrClient build = new HttpSolrClient.Builder(BASE_URL).build();
        return build;
    }
//     "product_catalog_name": "幽默杂货",
//     "product_price": 18.9,
//     "product_name": "花儿朵朵彩色金属门后挂 8钩免钉门背挂钩2066",
//     "id": "1",
//     "product_picture": "2014032613103438.png",
    @Test
    public void search()throws Exception{
        HttpSolrClient solrClient = getSolrClient();
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q","product_catalog_name:幽默杂货");
        QueryResponse query = solrClient.query(solrQuery);
        SolrDocumentList results = query.getResults();
        for (SolrDocument result : results) {
            System.out.println(result.getFieldValue("product_catalog_name"));
            System.out.println(result.getFieldValue("product_price"));
            System.out.println(result.getFieldValue("product_name"));
            System.out.println(result.getFieldValue("id"));
            System.out.println(result.getFieldValue("product_picture"));
        }
    }
    @Test
    public void getHigh()throws Exception{
        HttpSolrClient solrClient = getSolrClient();
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q","product_catalog_name:时尚卫浴 AND product_name:时尚");
        solrQuery.setHighlight(true);
        solrQuery.addHighlightField("product_catalog_name");
        solrQuery.addHighlightField("product_name");
        solrQuery.setHighlightSimplePre("<span>");
        solrQuery.setHighlightSimplePost("</span>");
        QueryResponse query = solrClient.query(solrQuery);
        Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();
        SolrDocumentList results = query.getResults();
        for (SolrDocument result : results) {
            Object id = result.getFieldValue("id");
            Map<String, List<String>> stringListMap = highlighting.get(id);
            System.out.println(stringListMap.get("product_catalog_name"));
            System.out.println(stringListMap.get("product_name"));
        }
    }
    @Test
    public void addDoc()throws Exception{
        HttpSolrClient solrClient = getSolrClient();
        SolrInputDocument solrInputFields = new SolrInputDocument();
        solrInputFields.addField("product_catalog_name","窝窝头一块钱四个");
        solrInputFields.addField("product_price","123");
        solrInputFields.addField("id","123");
        solrInputFields.addField("product_picture","fsdfsdf.jpg");
        solrInputFields.addField("product_name","谁他妈买小米");
        solrClient.add(solrInputFields);
        solrClient.commit();
    }
    @Test
    public void delbyId()throws Exception{
        HttpSolrClient solrClient = getSolrClient();
        solrClient.deleteById("123");
        solrClient.commit();
    }
    @Test
    public void getById()throws Exception{
        HttpSolrClient solrClient = getSolrClient();
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q","id:123");
        QueryResponse query = solrClient.query(solrQuery);
        SolrDocumentList results = query.getResults();
        for (SolrDocument result : results) {
            System.out.println(result.getFieldValue("id"));
            System.out.println(result.getFieldValue("product_catalog_name"));
            System.out.println(result.getFieldValue("product_price"));
            System.out.println(result.getFieldValue("product_name"));
        }
    }
}
